package customerController

import (
	"WEB/models"
	"github.com/astaxie/beego/orm"
)

type LoginController struct {
	BaseController
}

func (this *LoginController) Get() {
	this.TplName = "customer/login.html"
}

func (this *LoginController) Post() {
	user := models.User{}
	if err := this.ParseForm(&user); err != nil {
		panic(err)
	} else {
		//check if is exsit
		sql := models.QueryByEqualField(user, "UserName", "UserPassword")
		var temp models.User
		err := orm.NewOrm().Raw(sql, user.UserName, user.UserPassword).QueryRow(&temp)
		if err != nil {
			this.TplName = "error.html"
			this.Data["msg"] = "账号密码错误"
			return
		}
		this.SetSession("Username", user.UserName)
		this.SetSession("isLogin", "true")
		this.SetSession("User", temp)
		this.Redirect("index", 302)
	}
}

func (this *LoginController) Logout() {
	session := this.GetSession("isLogin")
	if session == "true" {
		this.SetSession("isLogin", false)
		this.SetSession("Username", "")
		this.SetSession("User", nil)
		this.Redirect("/customer/index", 302)
	} else {
		this.TplName = "error.html"
		this.Data["msg"] = "你还没有登录"
	}
}
